package cn.zhku.edu.orderservice.controller;

import cn.zhku.edu.feign.common.R;
import cn.zhku.edu.feign.pojo.bo.Orderdetail;
import cn.zhku.edu.feign.pojo.entry.Order;
import cn.zhku.edu.orderservice.service.OrderServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;


/**
 * @author clk
 * @Date:2021/9/13  17:01
 **/
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderServies orderServies;

    @RequestMapping("/getUserOrder")
    public R getUserOrder(@RequestParam String uid) {
        return new R(200, "访问成功！", orderServies.selectAllOrder(uid));
    }

    //http://localhost:8082/order/createOrder?uid=1&sum=2&phone=11111111111&receiver=wqq&address=123
    @RequestMapping("/createOrder")
    public R createOrder(@RequestParam String uid,@RequestParam int sum, String phone, String receiver, String address) {
        Order order = new Order();
        order.setUid(uid);
        order.setStatus("未完成");
        order.setSum(sum);

        java.util.Date date = new java.util.Date();
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timeStamp = Timestamp.valueOf(simpleDate.format(date));
        order.setTime(timeStamp);
        order.setPhone(phone);
        order.setReceiver(receiver);
        order.setAddress(address);
        orderServies.insert(order);
        int oid = order.getOid();
        return new R(200,"订单创建成功，返回订单id"+oid,oid);
    }

    //http://localhost:8082/order/insertOrderDetail?oid=11111&gid=6&count=6
    @RequestMapping("/insertOrderDetail")
    public R insertOrderDetail(@RequestParam Integer oid, Integer gid,Integer count){
        Orderdetail orderdetail = new Orderdetail();
        orderdetail.setOid(oid);
        orderdetail.setGid(gid);
        orderdetail.setCount(count);
        return new R(200,"成功插入订单细节",orderServies.insertdetail(orderdetail));
    }


    @RequestMapping("/deleteUserOrder")
    public R deleteUserOrder(@RequestParam Integer oid, String uid){
        Order myorder=new Order();
        myorder.setOid(oid);
        myorder.setUid(uid);
        myorder.setStatus("未完成");
        return new R(200,"取消订单成功",orderServies.deleteByPrimaryKey(myorder));
    }

    //http://localhost:8082/order/updateUserOrder?oid=22222&uid=1
    @RequestMapping("/updateUserOrder")
    public R updateUserOrder(@RequestParam Integer oid, String uid){
        Order myorder=new Order();
        myorder.setOid(oid);
        myorder.setUid(uid);
        myorder.setStatus("已完成");
        return new R(200,"支付成功",orderServies.updateByPrimaryKeySelective(myorder));
    }
    @RequestMapping("/test")
    public R test(){
        return new R(200,"支付成功",orderServies.selectByPrimaryKey("1"));
    }
}
